package com.erli.exchange.common.interceptor.db;

import java.sql.Statement;
import java.util.Properties;

import org.apache.ibatis.executor.resultset.ResultSetHandler;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Plugin;
import org.apache.ibatis.plugin.Signature;
import org.springframework.stereotype.Component;

import lombok.extern.slf4j.Slf4j;

@Component
@Intercepts({
    @Signature(type = ResultSetHandler.class, method = "handleResultSets", args={Statement.class})
})
@Slf4j
public class ResultInterceptor implements Interceptor {

	@Override
	public Object intercept(Invocation invocation) throws Throwable {
	    log.info("拦截器ResultInterceptor");
//	   // ResultSetHandler resultSetHandler1 = (ResultSetHandler) invocation.getTarget();
//	    //通过java反射获得mappedStatement属性值
//	    //可以获得mybatis里的resultype
//	    Object result = invocation.proceed();
//	    if (result instanceof ArrayList) {
//	        ArrayList resultList = (ArrayList) result;
//	        for (int i = 0; i < resultList.size(); i++) {
//	            Object oi = resultList.get(i);
//	            Class c = oi.getClass();
//	            Class[] types = {String.class};
//	            Method method = c.getMethod("setAddress", types);
//	            // 调用obj对象的 method 方法
//	            method.invoke(oi, "china");
//	        }
//	    }
	    return invocation.proceed();
	}
	
	@Override
	public Object plugin(Object target) {
	    return Plugin.wrap(target, this);
	}
	
	@Override
	public void setProperties(Properties properties) {
	
	}
}
